﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using CloudSharingStation_UWP_.Model;
using Windows.Storage.Pickers;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace CloudSharingStation_UWP_
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class SearchPage : Page
    {
        private ResourceList resourceList = null;
        public SearchPage()
        {
            this.InitializeComponent();
            pgrSearch.IsActive = false;
            pgbDownLoad.Visibility = Visibility.Collapsed;
            lblAlert.Visibility = Visibility.Visible;
        }

        private void Select_All_Checked(object sender, RoutedEventArgs e)
        {
            cbxExcel.IsChecked = cbxMp4.IsChecked = cbxPdf.IsChecked = cbxPpt.IsChecked = cbxRar.IsChecked = cbxTxt.IsChecked = cbxWord.IsChecked = true;
        }

        private void Select_All_Unchecked(object sender, RoutedEventArgs e)
        {
            cbxExcel.IsChecked = cbxMp4.IsChecked = cbxPdf.IsChecked = cbxPpt.IsChecked = cbxRar.IsChecked = cbxTxt.IsChecked = cbxWord.IsChecked = false;
        }

        private async void btnSearch_Clicked(object sender, RoutedEventArgs e)
        {
            if (cbxExcel.IsChecked==false&&cbxMp4.IsChecked==false&&cbxPdf.IsChecked==false&&cbxPpt.IsChecked==false&&cbxRar.IsChecked==false
                && cbxTxt.IsChecked == false)
            {
                ContentDialog dialog = new ContentDialog();
                dialog.Title = "错误";
                dialog.Content = "请指定文件格式";
                dialog.CloseButtonText = "确定";
                var result = dialog.ShowAsync();
            }
            else if(cmbDepartment.SelectedItem==null||cmbType.SelectedItem==null)
            {
                ContentDialog dialog = new ContentDialog();
                dialog.Title = "错误";
                dialog.Content = "请指定学院和类型";
                dialog.CloseButtonText = "确定";
                var result = dialog.ShowAsync();
            }
            else
            {
                lsvResource.Items.Clear();
                lblAlert.Visibility = Visibility.Collapsed;
                List<string> fileCatgories = new List<string>();
                string keyWord = null;
                string department = null;
                if (txtName.Text != null) keyWord = txtName.Text;
                if ((string)cmbDepartment.SelectedItem != "全部") department = (string)cmbDepartment.SelectedItem;
                if (cbxAll.IsChecked == true) fileCatgories = null;
                else
                {
                    if (cbxExcel.IsChecked == true) { fileCatgories.Add(".xls"); fileCatgories.Add(".xlsx"); }
                    if (cbxMp4.IsChecked == true) fileCatgories.Add(".mp4");
                    if (cbxPdf.IsChecked == true) fileCatgories.Add(".pdf");
                    if (cbxPpt.IsChecked == true) { fileCatgories.Add(".ppt");fileCatgories.Add(".pptx"); }
                    if (cbxRar.IsChecked == true) fileCatgories.Add(".rar");
                    if (cbxTxt.IsChecked == true) fileCatgories.Add(".txt");
                    if (cbxWord.IsChecked == true) { fileCatgories.Add(".doc");fileCatgories.Add(".docx"); }
                }
                pgrSearch.IsActive = true;
                SearchCondition searchConditon = new SearchCondition(department,keyWord,fileCatgories);
                Task<List<StudyResource>> task = Task<List<StudyResource>>.Run(() => resourceList.SearchFileByConditions(searchConditon));
                await task;
                List<StudyResource> allFiles = task.Result; // 从后端获取所有资源
                pgrSearch.IsActive = false;
                foreach (StudyResource item in allFiles)
                {
                    lsvResource.Items.Add(item);
                }
                if (lsvResource.Items.Count == 0) lblAlert.Visibility = Visibility.Visible;
            }
        }

        private async void btnDownLoadClick(object sender, RoutedEventArgs e)
        {
            FrameworkElement element = (FrameworkElement)sender;
            StudyResource selectedResource = (StudyResource)element.DataContext;
            //new MessageDialog("文件"+selectedResource.fileName+"已经开始下载", "下载").ShowAsync();
            FolderPicker picker = new FolderPicker();
            picker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
            picker.FileTypeFilter.Add("*");
            Windows.Storage.StorageFolder folder = await picker.PickSingleFolderAsync();
            if (folder != null)
            {
                string path = folder.Path+"\\";
                string fileName = selectedResource.fileName;
                txtDownloadAlert.Text = "正在下载"+fileName;
                pgbDownLoad.Visibility = Visibility.Visible;
                Task task = Task.Run(() => resourceList.DownloadFile(fileName, path)); //下载文件
                await task;
                pgbDownLoad.Visibility = Visibility.Collapsed;
                txtDownloadAlert.Text = "下载进度：下载完成";
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            resourceList = (ResourceList)e.Parameter;
        }
    }
}
